<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Content extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'content';
        $this->load->model('Mcontent');
    }
    
    public function channel()
    {
        $this->data['data'] = $this->Mcontent->getChannel();
        $this->data['action'] = 'channel';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addChannel()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('channel', 'Tên kênh', 'required');
        $this->form_validation->set_rules('logo_channel_', 'Logo kênh', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $channel = $this->getParamString('channel');
            $logo = $this->getParamString('logo_channel_');//die($logo);
            if(!in_array($logo, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'logo-channel-'.$timenow.'.jpg';
                $path = 'images/logo/'.date('Y-m').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$logo);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }
            
            $this->data['data'] = $this->Mcontent->addChannelDb($channel,$img);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/content/channel";</script>');
            }
        }
        $this->data['action'] = 'addchannel';
        $this->load->view('template/layout', $this->data);
    }
    
    
    public function editChannel()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('channel', 'Tên kênh', 'required');
        $this->form_validation->set_rules('logo_channel_check', 'Hình ảnh', 'required');
        
        $id = $this->getParamUri(4,1);
        $this->data['data'] = $this->Mcontent->getChannelInfo($id);
        
        if($this->form_validation->run() === TRUE)
        {
            $channel = $this->getParamString('channel');
            
            $logo_check = $this->getParamString('logo_channel_check');
            $logo_verify = $this->getParamString('logo_channel_verify');
            
            
            if(!in_array($logo_verify, array(null, '')))
            {
                if($logo_verify != $logo_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$logo_verify);
                }
            }
            
            $logo = $this->getParamString('logo_channel_');//die($logo);
            if(!in_array($logo, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'logo-channel-'.$timenow.'.png';
                $path = 'images/logo/'.date('Y-m').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$logo);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }            
            
            $this->data['data'] = $this->Mcontent->editChannelDb($channel,$img,$id);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/content/channel";</script>');
            }
        }
        $this->data['action'] = 'editchannel';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleteChannel()
    {
        $id = $this->getParamInt('id');
        $this->data['delete'] = $this->Mcontent->deleteChannelDb($id);
    }
    
    public function tvshow()
    {
		$channel_id = $this->getParamInt('channel_id');
		$this->data['channel_id'] = $channel_id;
        $this->data['data'] = $this->Mcontent->getTVShow($channel_id);
		$this->data['getChannel'] = $this->Mcontent->getChannel();
        $this->data['action'] = 'tvshow';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addTVShow()
    {        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('tvshow', 'Tên chương trình', 'required');
        $this->form_validation->set_rules('channel', 'Kênh phát sóng', 'required');
        $this->form_validation->set_rules('time_start', 'Giờ phát sóng', 'required');
        $this->form_validation->set_rules('background_tvshow_', 'Hình nền', 'required');
        
        $this->data['selectChannel'] = $this->Mcontent->getSelectChannel();
        if($this->form_validation->run() === TRUE)
        {
            $tvshow = $this->getParamString('tvshow');
            $channel = $this->getParamInt('channel');
            $time = $this->getParamString('time_start');
            
            $bground = $this->getParamString('background_tvshow_');//die($bground);
            if(!in_array($bground, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'background-'.$timenow.'.jpg';
                $path = 'images/background/'.date('Y-m').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$bground);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }
            
            $this->data['data'] = $this->Mcontent->addTVShowDb($tvshow,$channel,$time,$img);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/content/tvshow";</script>');
            }
        }
        
        $this->data['action'] = 'addtvshow';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editTVShow()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('tvshow', 'Tên chương trình', 'required');
        $this->form_validation->set_rules('channel', 'Kênh phát sóng', 'required');
        $this->form_validation->set_rules('time_start', 'Giờ phát sóng', 'required');
        $this->form_validation->set_rules('background_tvshow_check', 'Hình nền', 'required');
        
        $this->data['selectChannel'] = $this->Mcontent->getSelectChannel();
        $id = $this->getParamUri(4,1);
        $this->data['data'] = $this->Mcontent->getTVShowInfo($id);
        if($this->form_validation->run() === TRUE)
        {            
            $tvshow = $this->getParamString('tvshow');
            $channel = $this->getParamInt('channel');
            $time = $this->getParamString('time_start');
            
            $background_check = $this->getParamString('background_tvshow_check');
            $background_verify = $this->getParamString('background_tvshow_verify');
            
            $bground = $this->getParamString('background_tvshow_');//die($bground);
            if(!in_array($background_verify, array(null, '')))
            {
                if($background_verify != $background_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$background_verify);
                }
            }
            
            if(!in_array($bground, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'background-'.$timenow.'.jpg';
                $path = 'images/background/'.date('Y-m').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$bground);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }                   
            
            $this->data['data'] = $this->Mcontent->editTVShowDb($tvshow,$channel,$time,$img,$id);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/content/tvshow";</script>');
            }
        }
        
        $this->data['action'] = 'edittvshow';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleteTVShow()
    {
        $id = $this->getParamInt('id');
        $this->data['delete'] = $this->Mcontent->deleteTVShowDb($id);
    }
    
    public function addvote()
    {
        $this->data['id_tvshow'] = $this->getParamInt('id');
        $this->data['id_log'] = $this->getParamInt('id_log');
        $this->load->library('form_validation');
		$this->form_validation->set_rules('question', 'Nội dung câu hỏi', 'required');
        $this->form_validation->set_rules('choice', 'Đáp án', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $question           = $this->getParamString('question');
            $choice_            = $this->getParamArray('choice');
            $choice             = array();
            foreach($choice_ AS $el=>$le)
            {
                if(!in_array($le, array(null, '', '0')))
                {
                    $choice[] = $le;
                } 
            }
                  
            $this->data['data'] = $this->Mcontent->addVote($question, $choice, $this->data['id_tvshow'], $this->getParamInt('isshare'),$this->data['id_log']);
            
        }
        
        $this->load->view('template/content/addvote',$this->data);
    }
    
    public function getvotelist()
    {
        $this->data['tvshow_id'] = $this->getParamInt('tvshow_id');
        $this->data['data'] = $this->Mcontent->getVoteListDb($this->getParamInt('id'));
        $this->load->view('template/content/getvotelist', $this->data);
    }
    
    public function deleteVote()
    {
        $id = $this->getParamInt('id');
        $this->data['delete'] = $this->Mcontent->deleteVoteDb($id);
    }
}
